//
//  XZHMovieCollectionViewController.m
//  Piece
//
//  Created by dllo on 16/6/18.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "XZHMovieCollectionViewController.h"
#import "DYJDataBaseTool.h"
#import "UIImageView+WebCache.h"
#import "XZHMovieDetailModel.h"
#import "DYJCollectionWebViewController.h"
#import "XZHMovieDetailViewController.h"
#import "XZHWebViewController.h"

@interface XZHMovieCollectionViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, strong) UIImageView *noCollectionImageView;
@end

@implementation XZHMovieCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self setUpTableView];
    [self getDataFromDataBase];
    [self createTitle:@"电影"];
}

- (void)setUpTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 69) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _noCollectionImageView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 20, 360, 240)];
    _noCollectionImageView.image = [UIImage imageNamed:@"collection"];
    [_tableView addSubview:_noCollectionImageView];

  
    [self.view addSubview:_tableView];
}

- (void)getDataFromDataBase
{
    DYJDataBaseTool *dataBaseTool = [DYJDataBaseTool shareDYJDataBaseTool];
    NSMutableArray *array = [dataBaseTool selectAllMovie];
    self.dataSource = [NSMutableArray arrayWithArray:array];
    if (_dataSource.count == 0) {
        _noCollectionImageView.hidden = NO;
    }
    else
    {
        _noCollectionImageView.hidden = YES;
    }

    [_tableView reloadData];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark 设置cell支持的可编辑类型
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //判断编辑样式
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除数组对应的元素
        DYJDataBaseTool *dataBaseTool = [DYJDataBaseTool shareDYJDataBaseTool];
        [dataBaseTool delegateDataMovieId:[[self.dataSource objectAtIndex:indexPath.row] movie_id]];
        [self getDataFromDataBase];
        if (_dataSource.count == 0) {
            _noCollectionImageView.hidden = NO;
        }
        else
        {
            _noCollectionImageView.hidden = YES;
        }

        //刷新tableView
        [_tableView reloadData];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    XZHMovieDetailModel *model = [self.dataSource objectAtIndex:indexPath.row];
    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.image = [UIImage imageNamed:@"tab_movie_selected"];
    cell.textLabel.text = model.title;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    XZHMovieDetailModel *model = [self.dataSource objectAtIndex:indexPath.row];
    
    XZHWebViewController *webVC = [[XZHWebViewController alloc] init];
    webVC.url = model.web_url;
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
